package utils.global.singlework;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class WorkManager {

    private ActorSystem system;

    public WorkManager() {
        system = ActorSystem.create("UTILSSYSTEM");
    }

    private ConcurrentHashMap<String, ActorRef> POOL = new ConcurrentHashMap<>();

    public void createWork(String name, Class<? extends BaseWork> type) {
        POOL.put(name, system.actorOf(Props.create(type)));
    }

    public void pushWork(String name, Object msg) {
        ActorRef actorRef = POOL.get(name);
        actorRef.tell(msg, ActorRef.noSender());
    }
}
